package csu.webcourse.mypetstore.web.servlet;

import csu.webcourse.mypetstore.domain.Account;
import csu.webcourse.mypetstore.domain.Order;
import csu.webcourse.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class ShippingAddressServlet extends HttpServlet {
    private static final String CONFIRM_ORDER_FORM = "/WEB-INF/jsp/order/confirmOrder.jsp";
    private static final String SHIPPINGFORM = "/WEB-INF/jsp/order/shippingForm.jsp";
    private Order order;
    private String shipToFirstName;
    private String shipToLastName;
    private String shipAddress1;
    private String shipAddress2;
    private String shipCity;
    private String shipState;
    private String shipZip;
    private String shipCountry;
    private String msg;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        shipToFirstName = request.getParameter("shipToFirstName");
        shipToLastName = request.getParameter("shipToLastName");
        shipAddress1 = request.getParameter("shipAddress1");
        shipAddress2 = request.getParameter("shipAddress2");
        shipCity = request.getParameter("shipCity");
        shipState = request.getParameter("shipState");
        shipZip = request.getParameter("shipZip");
        shipCountry = request.getParameter("shipCountry");

        HttpSession session = request.getSession();
        if(!validate()) {
            request.setAttribute("msg", this.msg);
            request.getRequestDispatcher(SHIPPINGFORM).forward(request, response);
        }else {
            order = (Order)session.getAttribute("order");
            order.setShipToFirstName(shipToFirstName);
            order.setShipToLastName(shipToLastName);
            order.setShipAddress1(shipAddress1);
            order.setShipAddress2(shipAddress2);
            order.setShipCity(shipCity);
            order.setShipState(shipState);
            order.setShipZip(shipZip);
            order.setShipCountry(shipCountry);
            session.setAttribute("order", order);
            Account account = (Account)session.getAttribute("loginAccount");
            if (account != null) {
                HttpServletRequest httpRequest = request;
                String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                        + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

                LogService logService = new LogService();
                //最后加入的信息“XXXXX”应当为该界面的信息以及一些商品信息
                String logInfo = logService.logInfo(" ") + strBackUrl + "用户填写完收货地址，前往确认界面";
                logService.insertLogInfo(account.getUsername(), logInfo);
            }

            response.setContentType("text/plain");
            response.setHeader("Access-Control-Allow-Origin", "*");
            PrintWriter out = response.getWriter();
            out.print("view");
        }
    }

    private boolean validate() {
        if(shipAddress1 == null || shipAddress1.equals("")) {
            this.msg = "收货地址未填";
            return false;
        }
        if(shipCity == null || shipCity.equals("")) {
            this.msg = "城市未填";
            return false;
        }
        if(shipState == null || shipState.equals("")) {
            this.msg = "state未填";
            return false;
        }
        if(shipZip == null || shipZip.equals("")) {
            this.msg = "zip未填";
            return false;
        }
        if(shipCountry == null || shipCountry.equals("")) {
            this.msg = "国家未填";
            return false;
        }
        if(shipToFirstName == null || shipToFirstName.equals("")) {
            this.msg = "FirstName未填";
            return false;
        }
        if(shipToLastName == null || shipToLastName.equals("")) {
            this.msg = "LastName未填";
            return false;
        }
        return true;
    }
}